package com.zr.task.activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.base.CountTimeBaseActivity;
import com.zh.baselibrary.constant.ExtraName;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.baselibrary.utils.PhoneUtil;
import com.zh.baselibrary.utils.TTSUtils;
import com.zh.model.task.SubjectBean;
import com.zh.model.task.error.ErrataSwipeResBean;
import com.zh.model.task.error.ErrorDetailVOBean;
import com.zr.task.R;
import com.zr.task.view.SubjectView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class SubjectActivity extends CountTimeBaseActivity implements SubjectView.ToOpen {

    @BindView(R.id.linearLayout_contain)
    LinearLayout subjectLayout;
    private ErrataSwipeResBean mBean;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_subject;
    }

    @Override
    protected void init() {
        TTSUtils.getInstance().speak(getString(R.string.choose_error_subject));
        mBean = (ErrataSwipeResBean) getIntent().getSerializableExtra(ExtraName.KEY_DATA);
        setDatas(mBean.getSubjectVOS());
    }
    public void setDatas(List<SubjectBean> list) {
        subjectLayout.removeAllViews();
        for (SubjectBean subjectBean : mBean.getSubjectVOS()) {
            SubjectView view = new SubjectView(this, subjectBean, "", this);
            view.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            subjectLayout.addView(view);
        }
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        finish();
    }

    boolean isClicked = false;
    SubjectBean subjectBean;
    @Override
    public void toOpen(SubjectBean subjectBean, String name) {
        if (!isClicked) {
            this.subjectBean = subjectBean;
            Bundle bundle = new Bundle();
            ErrorDetailVOBean bean = new ErrorDetailVOBean(subjectBean.getCabinetNum(), PhoneUtil.getUUID(this),mBean.getStudentId());
            bundle.putSerializable(ExtraName.KEY_DATA,bean);
            IntentUtils.instance().startActivityForResult(this,CorrigendumDetailActivity.class,CorrigendumDetailActivity.RELATION_HOMEWORK_CODE,bundle);
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == CorrigendumDetailActivity.RESULT_CODE_COMPLTE) {
            setResult(CorrigendumDetailActivity.RESULT_CODE_COMPLTE, getIntent());
            mBean.getSubjectVOS().remove(subjectBean);
            setDatas(mBean.getSubjectVOS());
            if (mBean.getSubjectVOS().size() == 0) {
                finish();
            }
        }
    }
}
